package com.oyjd.beauty.fw.tool.bean;

import java.util.List;

import com.oyjd.beauty.fw.util.FileOP;



public class BeanOut {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String path = "N:/mystore/BeautyServer/script/table.sql";
		String outPath = "N:/mystore/BeautyServer/code/src/main/java";
		List<TableVO> list = DbScriptMysqlParse.parse(path, "gb2312");
		for (TableVO vo : list) {
			outPO(vo, outPath, "com.oyjd.beauty.bean.po", true);
			outVO(vo, outPath, "com.oyjd.beauty.bean.vo", true);
			// outJson(vo);
			// outDesc(vo);
			// outSet(vo);
		}
		outRO(list, outPath, "com.oyjd.beauty.bean.ro", true);

	}

	private static String NUM = "15072837524091";

	private static String getUID(String pck, String name) {
		String s = Integer.toString((pck + "." + name).hashCode());
		int len = s.length();
		if (len < 19) {
			s += NUM.substring(0, 19 - len);
		}
		return s + "L";
	}

	public static void outJson(TableVO vo) {
		String name = vo.getTable().getName();
		if (name.startsWith("T_")) {
			name = name.substring(2);
		}
		name = getFBE(name);
		StringBuffer sb = new StringBuffer();
		sb.append("-------------------------->" + name + "[" + vo.getTable().getDesc() + "];\r\n");
		sb.append("{");
		for (ColVO v : vo.getCols()) {
			sb.append("\"" + getFLE(v.getName()) + "\":\"xxx\",");
		}
		sb.append("}");
		System.out.println(sb);
	}

	public static void outDesc(TableVO vo) {
		String name = vo.getTable().getName();
		if (name.startsWith("T_")) {
			name = name.substring(2);
		}
		name = getFBE(name);
		StringBuffer sb = new StringBuffer();
		sb.append("-------------------------->" + name + "[" + vo.getTable().getDesc() + "][共" + vo.getCols().size() + "字段]\r\n");
		for (ColVO v : vo.getCols()) {
			sb.append(getFLE(v.getName()) + " " + getTypeName(v.getType()) + " " + v.getDesc() + "\r\n");
		}
		System.out.println(sb);
	}

	public static void outSet(TableVO vo) {
		String name = vo.getTable().getName();
		if (name.startsWith("T_")) {
			name = name.substring(2);
		}
		name = getFBE(name);
		StringBuffer sb = new StringBuffer();
		sb.append("---------------------------\r\n");
		sb.append(name + " " + getFL(name) + " = new " + name + "();\r\n");
		for (ColVO v : vo.getCols()) {
			sb.append(getFL(name) + ".set" + getFBE(v.getName()) + "();\r\n");
		}
		System.out.println(sb);
	}

	public static void outRO(List<TableVO> list, String outPath, String pkg, boolean outFileFlag) {
		outPath = outPath + "/" + pkg.replaceAll("\\.", "/");
		StringBuffer sb = new StringBuffer();
		sb.append("package " + pkg + ";\r\n\r\n");

		sb.append("import java.util.ArrayList;\r\n");
		sb.append("import java.util.List;\r\n");

		sb.append(getHeadDesc("报表头结构信息"));
		sb.append("public class ReportHead {\r\n\r\n");
		StringBuffer sb1 = new StringBuffer();
		StringBuffer sb2 = new StringBuffer();
		for (TableVO vo : list) {
			String name = vo.getTable().getName();
			if (name.startsWith("T_")) {
				name = name.substring(2);
			}
			sb1.append("	public static List<ColVO> " + name + " = new ArrayList<ColVO>(); //" + vo.getTable().getDesc() + "\r\n");
			for (ColVO v : vo.getCols()) {
				String d = v.getDesc();
				int p = d.indexOf("：");
				if (p != -1) {
					d = d.substring(0, p);
				}
				sb2.append("		" + name + ".add(new ColVO(\"" + getFLE(v.getName()) + "\", \"" + d + "\"));\r\n");
			}
			sb2.append("\r\n		//-------------------------------------------\r\n");
		}

		sb.append(sb1);
		sb.append("\r\n	static {\r\n");
		sb.append(sb2);
		sb.append("}\r\n");
		sb.append("}\r\n");
		if (outFileFlag) {
			String p = outPath + "/ReportHead.java";
			System.out.println(p);
			FileOP.writeFile(p, sb.toString(), "utf-8");
			writeColVO(outPath, pkg);
		} else {
			System.out.println(sb);
		}
	}

	private static void writeColVO(String outPath, String pkg) {
		StringBuffer sb = new StringBuffer();
		sb.append("package " + pkg + ";\r\n");
		sb.append("\r\n");
		sb.append("/**\r\n");
		sb.append(" * \r\n");
		sb.append(" * 字段信息对象\r\n");
		sb.append(" * \r\n");
		sb.append(" * @author <b>欧建东<br/>\r\n");
		sb.append(" *         Email:oyjd@163.com</b>\r\n");
		sb.append(" * @version 1.0\r\n");
		sb.append(" */\r\n");
		sb.append("public class ColVO {\r\n");
		sb.append("	/**\r\n");
		sb.append("	 * 字段代号\r\n");
		sb.append("	 */\r\n");
		sb.append("	private String code;\r\n");
		sb.append("	/**\r\n");
		sb.append("	 * 字段中文\r\n");
		sb.append("	 */\r\n");
		sb.append("	private String name;\r\n");
		sb.append("\r\n");
		sb.append("	public ColVO(String code, String name) {\r\n");
		sb.append("		super();\r\n");
		sb.append("		this.code = code;\r\n");
		sb.append("		this.name = name;\r\n");
		sb.append("	}\r\n");
		sb.append("\r\n");
		sb.append("	public String getCode() {\r\n");
		sb.append("		return code;\r\n");
		sb.append("	}\r\n");
		sb.append("\r\n");
		sb.append("	public void setCode(String code) {\r\n");
		sb.append("		this.code = code;\r\n");
		sb.append("	}\r\n");
		sb.append("\r\n");
		sb.append("	public String getName() {\r\n");
		sb.append("		return name;\r\n");
		sb.append("	}\r\n");
		sb.append("\r\n");
		sb.append("	public void setName(String name) {\r\n");
		sb.append("		this.name = name;\r\n");
		sb.append("	}\r\n");
		sb.append("\r\n");
		sb.append("}\r\n");
		String p = outPath + "/ColVO.java";
		System.out.println(p);
		FileOP.writeFile(p, sb.toString(), "utf-8");
	}

	private static String getTypeName(String type) {
		String s = "";
		if ("String".equals(type)) {
			s = "字符串";
		} else if ("int".equals(type)) {
			s = "整型";
		} else if ("float".equals(type)) {
			s = "浮点型";
		} else if ("long".equals(type)) {
			s = "长整型";
		} else if ("double".equals(type)) {
			s = "双精度";
		} else if ("Date".equals(type)) {
			s = "日期";
		}
		return s;
	}

	/**
	 * 
	 * @param vo
	 * @param outPath
	 *            输出路径
	 * @param pkg
	 *            包名
	 * @param outFileFlag
	 *            是否输出文件 false-表示仅仅控制台输出
	 */
	public static void outPO(TableVO vo, String outPath, String pkg, boolean outFileFlag) {
		outPath = outPath + "/" + pkg.replaceAll("\\.", "/");
		StringBuffer sb = new StringBuffer();
		StringBuffer sb2 = new StringBuffer();
		boolean dateFlag = false;
		for (ColVO cvo : vo.getCols()) {
			if ("Date".equals(cvo.getType())) {
				dateFlag = true;
				break;
			}
		}
		outHeadPO(sb, vo.getTable(), pkg, dateFlag);
		for (ColVO cvo : vo.getCols()) {
			outBeanPO(sb, cvo);
		}
		for (ColVO cvo : vo.getCols()) {
			outGetSet(sb, cvo);
		}
		sb.append(sb2);
		sb.append("}");

		if (outFileFlag) {
			String name = vo.getTable().getName();
			if (name.startsWith("T_")) {
				name = name.substring(2);
			}
			String p = outPath + "/" + getFBE(name) + ".java";
			System.out.println(p);
			FileOP.writeFile(p, sb.toString(), "utf-8");
		} else {
			System.out.println(sb);
		}
	}

	/**
	 * 
	 * @param vo
	 * @param outPath
	 *            输出路径
	 * @param pkg
	 *            包名
	 * @param outFileFlag
	 *            是否输出文件 false-表示仅仅控制台输出
	 */
	public static void outVO(TableVO vo, String outPath, String pkg, boolean outFileFlag) {
		outPath = outPath + "/" + pkg.replaceAll("\\.", "/");
		StringBuffer sb = new StringBuffer();
		StringBuffer sb2 = new StringBuffer();
		boolean dateFlag = false;
		for (ColVO cvo : vo.getCols()) {
			if ("Date".equals(cvo.getType())) {
				dateFlag = true;
				break;
			}
		}
		outHeadVO(sb, vo.getTable(), pkg, dateFlag);
		for (ColVO cvo : vo.getCols()) {
			outBeanVO(sb, cvo);
		}
		outStruct(sb, vo.getTable().getName(), vo.getCols());
		for (ColVO cvo : vo.getCols()) {
			outGetSet(sb, cvo);
		}
		sb.append(sb2);
		sb.append("}");

		if (outFileFlag) {
			String name = vo.getTable().getName();
			if (name.startsWith("T_")) {
				name = name.substring(2);
			}
			String p = outPath + "/" + getFBE(name) + "VO.java";
			System.out.println(p);
			FileOP.writeFile(p, sb.toString(), "utf-8");
		} else {
			System.out.println(sb);
		}
	}

	private static void outStruct(StringBuffer sb, String name, List<ColVO> cols) {
		if (name.startsWith("T_")) {
			name = name.substring(2);
		}
		name = getFBE(name);
		sb.append("	public " + name + "VO() {\r\n");
		sb.append("		super();\r\n");
		sb.append("	}\r\n\r\n");

		StringBuffer sb1 = new StringBuffer();
		StringBuffer sb2 = new StringBuffer();
		StringBuffer sb3 = new StringBuffer();
		for (ColVO vo : cols) {
			String a = getFLE(vo.getName());
			sb1.append(", " + vo.getType() + " " + a);
			sb2.append("		this." + a + " = " + a + ";\r\n");
			sb3.append(", " + name.toLowerCase() + "." + a);
		}
		sb.append("	public static final String STRUCT = \"(" + sb3.toString().substring(2) + ")\";\r\n\r\n");
		sb.append("	public " + name + "VO(" + sb1.toString().substring(2) + ") {\r\n");
		sb.append("		super();\r\n");
		sb.append(sb2.toString());
		sb.append("	}\r\n\r\n");
	}

	/**
	 * 
	 * @param sb
	 * @param vo
	 * @param dateFlag
	 *            是否含日期
	 */
	private static void outHeadPO(StringBuffer sb, ColVO vo, String pkg, boolean dateFlag) {
		sb.append("package " + pkg + ";\r\n\r\n");

		sb.append("import java.io.Serializable;\r\n");
		if (dateFlag) {
			sb.append("import java.util.Date;\r\n");
		}

		sb.append("\r\nimport javax.persistence.Column;\r\n");
		sb.append("import javax.persistence.Entity;\r\n");
		sb.append("import javax.persistence.GeneratedValue;\r\n");
		sb.append("import javax.persistence.Id;\r\n");
		sb.append("import javax.persistence.Table;\r\n");
		if (dateFlag) {
			sb.append("import javax.persistence.Temporal;\r\n");
			sb.append("import javax.persistence.TemporalType;\r\n");
		}

		sb.append("\r\nimport org.hibernate.annotations.GenericGenerator;\r\n\r\n");

		sb.append(getHeadDesc(vo.getDesc() + "实体"));
		sb.append("@Entity\r\n");
		sb.append("@Table(name = \"" + vo.getName() + "\")\r\n");
		String name = vo.getName();
		if (name.startsWith("T_")) {
			name = name.substring(2);
		}
		sb.append("public class " + getFBE(name) + " implements Serializable {\r\n\r\n");
		sb.append("	/**\r\n");
		sb.append("	 * 序列号\r\n");
		sb.append("	 */\r\n");
		sb.append("	private static final long serialVersionUID = " + getUID("com.zmcn.business.po", getFBE(name)) + ";\r\n\r\n");

	}

	/**
	 * 
	 * @param sb
	 * @param vo
	 * @param dateFlag
	 *            是否含日期
	 */
	private static void outHeadVO(StringBuffer sb, ColVO vo, String pkg, boolean dateFlag) {
		sb.append("package " + pkg + ";\r\n\r\n");

		if (dateFlag) {
			sb.append("import java.util.Date;\r\n");
		}

		sb.append(getHeadDesc(vo.getDesc() + "值对象"));
		String name = vo.getName();
		if (name.startsWith("T_")) {
			name = name.substring(2);
		}
		sb.append("public class " + getFBE(name) + "VO {\r\n\r\n");
	}

	/**
	 * 获得头注释块
	 * 
	 * @param title
	 * @return
	 */
	private static String getHeadDesc(String title) {
		StringBuffer sb = new StringBuffer();
		sb.append("/**\r\n");
		sb.append(" * \r\n");
		sb.append(" * ").append(title).append("\r\n");
		sb.append(" * \r\n");
		sb.append(" * @author <b>欧建东<br/>\r\n");
		sb.append(" *         Email:oyjd@163.com</b>\r\n");
		sb.append(" * @version 1.0\r\n");
		sb.append(" */\r\n");
		return sb.toString();
	}

	public static void outBeanPO(StringBuffer sb, ColVO vo) {
		sb.append("	/**\r\n");
		sb.append("	 * " + vo.getDesc() + "\r\n");
		sb.append("	 */\r\n");
		if (vo.isPk()) {
			sb.append("	@Id\r\n");
			sb.append("	@GeneratedValue(generator = \"system-uuid\")\r\n");
			sb.append("	@GenericGenerator(name = \"system-uuid\", strategy = \"uuid\")\r\n");
		}

		if ("String".equals(vo.getType())) {
			sb.append("	@Column(name = \"" + vo.getName() + "\", length = " + vo.getLen() + ")\r\n");
			sb.append("	private String " + getFLE(vo.getName()) + ";\r\n\r\n");
		} else if ("Date".equals(vo.getType())) {
			sb.append("	@Temporal(TemporalType.TIMESTAMP)\r\n");
			sb.append("	@Column(name = \"" + vo.getName() + "\")\r\n");
			sb.append("	private " + vo.getType() + " " + getFLE(vo.getName()) + ";\r\n\r\n");
		} else {
			sb.append("	@Column(name = \"" + vo.getName() + "\")\r\n");
			sb.append("	private " + vo.getType() + " " + getFLE(vo.getName()) + ";\r\n\r\n");
		}
	}

	public static void outBeanVO(StringBuffer sb, ColVO vo) {
		sb.append("	/**\r\n");
		sb.append("	 * " + vo.getDesc() + "\r\n");
		sb.append("	 */\r\n");

		if ("String".equals(vo.getType())) {
			sb.append("	private String " + getFLE(vo.getName()) + ";\r\n\r\n");
		} else if ("Date".equals(vo.getType())) {
			sb.append("	private " + vo.getType() + " " + getFLE(vo.getName()) + ";\r\n\r\n");
		} else {
			sb.append("	private " + vo.getType() + " " + getFLE(vo.getName()) + ";\r\n\r\n");
		}
	}

	public static void outGetSet(StringBuffer sb, ColVO vo) {
		String a = getFBE(vo.getName());
		String b = getFLE(vo.getName());

		sb.append("	public " + vo.getType() + " get" + a + "() {\r\n");
		sb.append("		return " + b + ";\r\n");
		sb.append("	}\r\n\r\n");

		sb.append("	public void set" + a + "(" + vo.getType() + " " + b + ") {\r\n");
		sb.append("		this." + b + " = " + b + ";\r\n");
		sb.append("	}\r\n\r\n");
	}

	private static String getFL(String s) {
		return s.substring(0, 1).toLowerCase() + s.substring(1);
	}

	private static String getFB(String s) {
		return s.substring(0, 1).toUpperCase() + s.substring(1);
	}

	private static String getFBE(String s) {
		String[] sa = s.split("_");
		String t = "";
		for (String ss : sa) {
			t += getFB(ss.toLowerCase());
		}
		return t;
	}

	private static String getFLE(String s) {
		return getFL(getFBE(s));
	}
}
